package com.example.demo.dao.impl;

import com.example.demo.bean.User;
import com.example.demo.dao.UserDao;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Data
public class UserInpl implements UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    private boolean softDeleteSupported = false;
    public UserInpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
        // Detect soft delete column presence once
        try {
            Integer exists = jdbcTemplate.queryForObject(
                    "select 1 from information_schema.columns where table_schema = database() and table_name = 'user' and column_name = 'is_deleted' limit 1",
                    Integer.class
            );
            softDeleteSupported = (exists != null);
        } catch (Exception ignored) {
            softDeleteSupported = false;
        }
    }

    @Override
    public User getUserById(Integer id) {
        String sql = softDeleteSupported
                ? "select id, username, password, email from user where is_deleted = 0 and id = ?"
                : "select id, username, password, email from user where id = ?";
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), id);
        return user;
    }

    @Override
    public void addUser(User user) {
            String sql = softDeleteSupported
                    ? "insert into user(username,password,email,is_deleted) values(?,?,?,0)"
                    : "insert into user(username,password,email) values(?,?,?)";
            if (softDeleteSupported) {
                jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getEmail());
            } else {
                jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getEmail());
            }
        System.out.println("新增成功");
    }

    @Override
    public void updateUser(User user) {
        String sql = softDeleteSupported
                ? "update user set username = ?, password = ?, email = ? where is_deleted = 0 and id = ?"
                : "update user set username = ?, password = ?, email = ? where id = ?";
        jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getId());
        System.out.println("修改成功");
    }

    @Override
    public void deleteUserById(Integer id) {
        String sql = "delete from user where id = ?";
        jdbcTemplate.update(sql, id);
        System.out.println("删除成功");

    }

    @Override
    public void softDeleteUserById(Integer id) {
        if (!softDeleteSupported) {
            // Fallback to hard delete when soft delete column not present
            deleteUserById(id);
            return;
        }
        String sql = "update user set is_deleted = 1 where id = ?";
        jdbcTemplate.update(sql, id);
        System.out.println("软删除成功");
    }

    @Override
    public User getUserByName(String username) {
        String sql = "select * from user where username = ?";
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), username);
        return user;
    }

    @Override
    public List<User> getAllUser() {
            String sql = softDeleteSupported
                    ? "select id, username, password, email from user where is_deleted = 0"
                    : "select id, username, password, email from user";
            List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
            return users;
    }
}
